<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ServiceBlock.class.php, $Author:  $ */



/** Service with blocking/unblocking/get blocking information functions
 * 
 * @package module.account
 * @version $Id: PhpFox_ServiceBlock.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   
 */
class PhpFox_Mod_Account_ServiceBlock extends BaseService 
{

    /** Block target user
     *
     * @param PhpFox_Mod_Account_ItemAccount $oBlocker blocker user
     * @param PhpFox_Mod_Account_ItemAccount $oUser    user to block
     * @return bool true - block succesfull, false -- error occured
     */
    function block($oBlocker, $oUser)
    {
       $aVals = array(
           'user1' => $oBlocker->aData['user'],
           'user2' => $oUser->aData['user'],
           'time'  => time(),
       );
       return $oBlocker->oDb->insert(App::getT('block'), $aVals);
    }
    
    /** Unblock target user
     * 
     * @param PhpFox_Mod_Account_ItemAccount $oBlocker blocker user
     * @param PhpFox_Mod_Account_ItemAccount $oUser    user to unblock
     */
    function unblock($oBlocker, $oUser)
    {
        $oBlocker->oDb->query('DELETE FROM '.App::getT('block').
                              '  WHERE user1 = "'.$oBlocker->aData['user'].'"'.
                              '    AND user2 = "'.$oUser->aData['user'].'"');
        return true;
    }
    
    /** User target user blocked by given user
     * 
     * @param PhpFox_Mod_Account_ItemAccount $oBlocker blocker user (given user)
     * @param PhpFox_Mod_Account_ItemAccount $oUser    user to check on block/unblock (target user) 
     * @return bool true - user blocked, false - user not blocked
     */
    function isBlocked($oBlocker, $oUser)
    {
        $iCnt = 0;
        if (isset($oUser->aData) and isset($oBlocker->aData))
        {
            if (isset($oBlocker->aData['user']) and $oBlocker->aData['user'] and isset($oUser->aData['user']) and $oUser->aData['user'])
            {
            $iCnt = $oBlocker->oDb->getField('SELECT COUNT(*) FROM '.App::getT('block').
                                             '  WHERE user1 = "'.$oBlocker->aData['user'].'"'.
                                             '    AND user2 = "'.$oUser->aData['user'].'"');
            }
        }
        return ($iCnt > 0);
    }

    /** Return list of user blocked by given user
     * 
     * @param PhpFox_Mod_Account_ItemAccount $oUser     user (blocker)
     * @param int                            $iPage     page number
     * @param int                            $iPageSize page size (rows per page)
     * @param string                         $sOrder    order string
     */
    function getBlockedUsers(&$oUser, $iPage=0, $iPageSize=0, $sOrder = '')
    {
        $oBlock = $this->_oModule->getItem('Block');
        /* @var $oBlock PhpFox_Mod_Account_ItemBlock */
        $aCond = array('user1' => ' = "'.$oUser->aData['user'].'"');
        return $oBlock->getList($aCond, $iPage, $iPageSize, $sOrder);
    }
    
}
?>